/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.common.util.control;

import java.io.Serializable;

/**
 * 
 * <请简单说明此类用途>
 * 
 * @Title com.enfry.util.control.Eventee
 * @author Penghu
 * @date 2018年1月10日
 */
public final class Lock implements Serializable {
	private static final long serialVersionUID = 1L;
	private boolean isIdle = false;

	/** 当前线程是空闲 */
	public boolean isIdle() {
		return isIdle;
	}

	/** 当前线程是否在运行 */
	public boolean isActive() {
		return !isIdle;
	}

	/** 阻塞当前线程，直到收到其他线程发送通知为止 */
	public void waitNotify() {
		waitNotify(0);
	}

	/**
	 * 阻塞当前线程，直到收到其他线程发送通知或等待时间结束自动唤醒
	 * 
	 * @param timeout 等待时间，单位毫秒
	 */
	public synchronized void waitNotify(long timeout) {
		try {
			isIdle = true;
			wait(timeout);
			isIdle = false;// 正常notify
		} catch (InterruptedException ex) {
			isIdle = false;// 线程interrupt
		}
	}

	/** 结束阻塞线程，通知正在等待的线程结束阻塞线程 */
	public synchronized void notifyWait() {
		notify();
	}
}